package com.topisv.tms.web.phone;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.topisv.tms.entity.User;
import com.topisv.tms.entity.stock.CusStock;
import com.topisv.tms.service.account.AccountService;
import com.topisv.tms.service.stock.CusStockService;
import com.topisv.tms.utils.enums.StatusEnums;

import net.sf.json.JSONObject;
/**
 * 库存查询
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/phone")
public class StockPhoneController {
	private static final Logger log = LoggerFactory.getLogger(StockPhoneController.class);
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private CusStockService cusStockService;
	
	
	@RequestMapping(value = "ajaxStockQueue")
	public void ajaxSend(@RequestParam(value = "code") String code, 
			@RequestParam(value = "jobuuid") String jobuuid, 
			HttpServletResponse response) {
		JSONObject json = null;
		PrintWriter out = null;
		List<CusStock> cusStocks=null;
		String[] areaGroups=null;
		String[] customers=null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			cusStocks=new ArrayList<CusStock>();
			log.info("WMS库位查询");
			log.info("jobuuid:"+jobuuid);
			User user=accountService.findUserByJobuuid(jobuuid);
			if(user==null){
				json.put("status", 0);
				json.put("content", "该用户不存在");
				out.print(json.toString());
				return;
			}
			areaGroups=user.getAreagroups().split(":");//仓库信息
			if(StatusEnums.user_type_customer.getStatus().equals(user.getUserType())){//商家用户
				Map<String, Object> maps=new HashMap<String, Object>();
				maps.put("customerCode", user.getCustomerCode());
				maps.put("sku", code);
				for(String a:areaGroups){
					maps.put("warehouseCode", a);
					cusStocks.addAll(cusStockService.findStockByParam(maps));//code为SKU
				}
			}else if(StatusEnums.user_type_paiu.getStatus().equals(user.getUserType())){
				Map<String, Object> maps=new HashMap<String, Object>();
				maps.put("sku", code);
				customers=user.getCustomerCode().split(":");
				for(String a:areaGroups){
					  maps.put("warehouseCode", a);
					 for(String customer:customers){
						 maps.put("customerCode", customer);
						 cusStocks.addAll(cusStockService.findStockByParam(maps));//code为SKU
					 }
				}
			}
			
			if(cusStocks.isEmpty()){//把code当做库位查询
				if(StatusEnums.user_type_customer.getStatus().equals(user.getUserType())){//商家用户
					Map<String, Object> maps=new HashMap<String, Object>();
					maps.put("customerCode", user.getCustomerCode());
					maps.put("stockCode", code);
					for(String a:areaGroups){
						maps.put("warehouseCode", a);
						cusStocks.addAll(cusStockService.findStockByParam(maps));//code为库位
					}
				}else if(StatusEnums.user_type_paiu.getStatus().equals(user.getUserType())){
					Map<String, Object> maps=new HashMap<String, Object>();
					maps.put("stockCode", code);
					customers=user.getCustomerCode().split(":");
					for(String a:areaGroups){
						 maps.put("warehouseCode", a);
						 for(String customer:customers){
							 maps.put("customerCode", customer);
							 cusStocks.addAll(cusStockService.findStockByParam(maps));//code为库位
						 }
					}
				}
			}
			if(cusStocks.isEmpty()){
				json.put("status", 0);
				json.put("content", "没有信息");
				out.print(json.toString());
				return;
			}
			json.put("status", 1);
			json.put("content", cusStocks);
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			log.error("error",e);
		} 
		out.print(json.toString());
	}
}
